package com.chance.web.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chance.common.context.LoginUserContext;
import com.chance.model.entity.*;
import com.chance.model.enums.LeaseStatus;
import com.chance.web.app.mapper.*;
import com.chance.web.app.service.*;
import com.chance.web.app.vo.apartment.ApartmentItemVo;
import com.chance.web.app.vo.attr.AttrValueVo;
import com.chance.web.app.vo.fee.FeeValueVo;
import com.chance.web.app.vo.graph.GraphVo;
import com.chance.web.app.vo.room.RoomDetailVo;
import com.chance.web.app.vo.room.RoomItemVo;
import com.chance.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {


    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private ApartmentInfoService apartmentInfoService;

    @Autowired
    private GraphInfoMapper  graphInfoMapper;

    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper ;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    @Autowired
    private FeeValueMapper feeValueMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    @Autowired
    private BrowsingHistoryService browsingHistoryService;


    @Override
    public IPage<RoomItemVo> pageItem(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(page,queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {


        // 查询房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        // 查询公寓信息
        ApartmentItemVo apartmentItemVo = apartmentInfoService.selectByApartmentId(roomInfo.getApartmentId());
        // 查询房间图片
        List<GraphVo> graphVo = graphInfoMapper.selectListByRoomId(id);
        //查询属性信息
        List<AttrValueVo> attrValueVos = roomAttrValueMapper.selectByRoomId(id);
        // 查询配套信息
        List<FacilityInfo> facilityInfos = facilityInfoMapper.selectByRoomId(id);
        // 查询标签信息
        List<LabelInfo> labelInfos = labelInfoMapper.selectByRoomId(id);
        //查询支付方式信息
        List<PaymentType> paymentTypes = paymentTypeMapper.selectByRoomId(id);
        // 查询杂费信息
        List<FeeValueVo> feeValueVos = feeValueMapper.selectByRoomId(roomInfo.getApartmentId());
        // 查询租期信息
        List<LeaseTerm> leaseTerms = leaseTermMapper.selectByRoomId(id);
        // 查询是否已入住
        LambdaQueryWrapper<LeaseAgreement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LeaseAgreement::getRoomId, roomInfo.getId());
        queryWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING);
        Long singedCount = leaseAgreementMapper.selectCount(queryWrapper);

        // 组装数据
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setGraphVoList(graphVo);
        roomDetailVo.setAttrValueVoList(attrValueVos);
        roomDetailVo.setFacilityInfoList(facilityInfos);
        roomDetailVo.setLabelInfoList(labelInfos);
        roomDetailVo.setPaymentTypeList(paymentTypes);
        roomDetailVo.setFeeValueVoList(feeValueVos);
        roomDetailVo.setLeaseTermList(leaseTerms);
        roomDetailVo.setIsCheckIn(singedCount > 0);

        // 保存浏览记录
        browsingHistoryService.saveHistory(LoginUserContext.getLoginUser().getUserId(), id);



        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> pageItemByApartmentId(IPage<RoomItemVo> page, Long id) {
        return roomInfoMapper.pageItemByApartmentId(page, id);
    }
}




